/**
 * @author  Thomas Lehmann
 * @file    DefaultScannerTest.js
 * @note    //http://docs.busterjs.org/en/latest/modules/buster-assertions/
 *
 * Copyright (c) 2013 Thomas Lehmann
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
 * documentation files (the "Software"), to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
 * and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all copies
 * or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
 * DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
var buster         = require("buster");
var Class          = require("../src/ClassTools.js").Class;
var DefaultScanner = require("../src/DefaultScanner.js").DefaultScanner;

var Token = Class({
    initialize: function(id, code) {
        this.id   = id;
        this.code = code;
    }
});

var Collector = Class({
    initialize: function() {
        this.data = new Array();
    },

    addToken: function(id, code) {
        this.data.push(new Token(id, code))
    }
});

/**
    Testing class (function) "DefaultScanner".
 */
buster.testCase("Testing class DefaultScanner:", {
    /**
        Positive testing of DefaultScanner.scanLineComment(text, index) method.
     */
    "testing method 'scanLineComment' (should succeed)": function() {
        var collector = new Collector();
        var scanner   = new DefaultScanner(collector.addToken.bind(collector));
        var code      = "// this is a line comment";

        assert.equals(code.length, scanner.scanLineComment(code, 0));
        assert.equals(1, collector.data.length);
        assert.equals(code, collector.data[0].code);
        assert.equals("line-comment", collector.data[0].id);
    },

    /**
        Negative testing of DefaultScanner.scanLineComment(text, index) method.
     */
    "testing method 'scanLineComment' (force fail)": function() {
        var collector = new Collector();
        var scanner   = new DefaultScanner(collector.addToken.bind(collector));
        var code      = "/* hello world */";

        assert.equals(0, scanner.scanLineComment(code, 0));
        assert.equals(0, collector.data.length);
    },

    "testing 'scanDoubleQuotedString'": function() {
        var collector = new Collector();
        var scanner   = new DefaultScanner(collector.addToken.bind(collector));

        var code      = "\"hello world\"";
        assert.equals(code.length, scanner.scanDoubleQuotedString(code, 0));
        assert.equals(1, collector.data.length);
        assert.equals(code, collector.data[0].code);
        assert.equals("double-quoted-string", collector.data[0].id);

        code = "\"String \\\"in\\\" string\""; // "String in \"in\" string"
        assert.equals(code.length, scanner.scanDoubleQuotedString(code, 0));
        assert.equals(2, collector.data.length);
        assert.equals(code, collector.data[1].code);
        assert.equals("double-quoted-string", collector.data[1].id);
    }
});

